#
# example002.py - LOW PASS FILTER
#
# This example loads an audio wav file, LP Filter, next it plots their samples
# in time domain and its DFT (Discrete Fourier Transform) using FFT (Fast Fou-
# rier Transform)
#
# Copyright (C) 2012 Robert Buj Gelonch
# Copyright (C) 2012 David Megias Jimenez
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
__author__ = "Robert Buj Gelonch, and David Megias Jimenez"
__copyright__ = "Copyright 2012, Robert Buj Gelonch and David Megias Jimenez"
__credits__ = ["Robert Buj Gelonch", "David Megias Jimenez"]
__license__ = "GPL"
__version__ = "3"
__maintainer__ = "Robert Buj"
__email__ = "rbuj@uoc.edu"
__status__ = "Development"
__docformat__ = 'plaintext'

import sys

import getopt
from scipy.io.wavfile import read
from uoc.wave.filter import lpfilter
from uoc.wave.plot import filter

def example002(inputfile):
    print "example002.py"
    print
    #--------------------------------------------------------------
    # Read audio file
    #--------------------------------------------------------------
    (sample_rate, samples) = read(inputfile)
    #--------------------------------------------------------------
    # apply LP filter
    #--------------------------------------------------------------
    numtaps = 29
    cutoff_hz = 3000.0
    filtered_signal, fir_coeff, nyq_rate = lpfilter(samples, \
                                                    sample_rate, \
                                                    cutoff_hz, \
                                                    numtaps)
    #--------------------------------------------------------------
    # Plot
    #--------------------------------------------------------------
    plt = filter(filtered_signal, samples, sample_rate, numtaps, fir_coeff, \
                 nyq_rate, 'Low pass filter')
    # Save plot
    plt.savefig("../plots/example002.py.1.png", format="png")
    # Show plot
    plt.show()

def main(argv):
    inputfile = '../../../media/OSR_uk_000_0020_8k.wav'
    try:
        opts, args = getopt.getopt(argv, "hi:o:", ["ifile="])
    except getopt.GetoptError:
        print 'example002.py -i <inputfile>'
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print 'example002.py -i <inputfile>'
            sys.exit()
        elif opt in ("-i", "--ifile"):
            inputfile = arg
    example002(inputfile)

if __name__ == "__main__":
    main(sys.argv[1:])
    print "Done"